package com.springcloud.sc.ucpv5.client.commondata.data;

import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.springcloud.sc.ucpv5.client.commondata.datachange.DataExChange;
import com.google.protobuf.Message;
import lombok.Data;

/**
 * Generated from IDL struct "Group".
 *
 * @author JacORB IDL compiler V 2.3-beta-2, 14-Oct-2006
 * @version generated at 2014-10-29 11:48:54
 */
@Data
public final class Group extends DataExChange {
    public Group() {
    }

    public String GroupTelno = "";
    public String ParGrpTelno = "";
    public String GroupName = "";
    public int GroupType;
    public int Point_x;
    public int Point_y;
    public String ip = "";
    public int port;
    public int VoiceOpen;
    public int MessageOpen;
    public int DataSerOpen;
    public int Supplement;
    public int GCallTimer;
    public int Priority;
    public int FreeTimer;
    public int LETime;
    public String longitude = "";
    public String latitude = "";
    public String zoom = "";
    public int GroupTypeTmp;
    public String IconName = "";
    public int GroupNodeFlag;
    public String NodeNumber = "";
    public String keep1 = "";
    public String keep2 = "";
    public String keep3 = "";
    public String keep4 = "";

    // 子组列表
    public List<Group> sonGrpTelno = new ArrayList<Group>();
    
    // wsy 20190520 设置组相关信息
    public GroupDataInfo groupDataInfo = new GroupDataInfo();

    public Group(String GroupTelno, String ParGrpTelno, String GroupName, int GroupType, int Point_x, int Point_y,
                 String ip, int port, int VoiceOpen, int MessageOpen, int DataSerOpen, int Supplement, int GCallTimer, int Priority, int FreeTimer,
                 int LETime, String longitude, String latitude, String zoom, int GroupTypeTmp, String IconName,
                 int GroupNodeFlag, String NodeNumber, String keep1, String keep2, String keep3, String keep4) {
	this.GroupTelno = GroupTelno;
	this.ParGrpTelno = ParGrpTelno;
	this.GroupName = GroupName;
	this.GroupType = GroupType;
	this.Point_x = Point_x;
	this.Point_y = Point_y;
	this.ip = ip;
	this.port = port;
	this.VoiceOpen = VoiceOpen;
	this.MessageOpen = MessageOpen;
	this.DataSerOpen = DataSerOpen;
	this.Supplement = Supplement;
	this.GCallTimer = GCallTimer;
	this.Priority = Priority;
	this.FreeTimer = FreeTimer;
	this.LETime = LETime;
	this.longitude = longitude;
	this.latitude = latitude;
	this.zoom = zoom;
	this.GroupTypeTmp = GroupTypeTmp;
	this.IconName = IconName;
	this.GroupNodeFlag = GroupNodeFlag;
	this.NodeNumber = NodeNumber;
	this.keep1 = keep1;
	this.keep2 = keep2;
	this.keep3 = keep3;
	this.keep4 = keep4;
    }

    public Message dataChangeCommonToProBufData() {
	try {
	    com.proto.uuos_sdk_pb.Group.Builder builder_dst = com.proto.uuos_sdk_pb.Group.newBuilder();
	    builder_dst.setGroupTelno(GroupTelno);
	    builder_dst.setGroupName(GroupName);
	    builder_dst.setGroupType(GroupType);
	    builder_dst.setPointX(Point_x);
	    builder_dst.setPointY(Point_y);
	    builder_dst.setIp(ip);
	    builder_dst.setPort(port);
	    builder_dst.setVoiceOpen(VoiceOpen);
	    builder_dst.setMessageOpen(MessageOpen);
	    builder_dst.setDataSerOpen(DataSerOpen);
	    builder_dst.setSupplement(Supplement);
	    builder_dst.setGCallTimer(GCallTimer);
	    builder_dst.setPriority(Priority);
	    builder_dst.setFreeTimer(FreeTimer);
	    builder_dst.setLETime(LETime);
	    builder_dst.setLongitude(longitude);
	    builder_dst.setLatitude(latitude);
	    builder_dst.setZoom(zoom);
	    builder_dst.setGroupTypeTmp(GroupTypeTmp);
	    builder_dst.setIconName(IconName);
	    builder_dst.setGroupNodeFlag(GroupNodeFlag);
	    builder_dst.setNodeNumber(NodeNumber);
	    if(keep1 != null) {
			builder_dst.setKeep1(keep1);
		}
	    builder_dst.setKeep2(keep2);
	    builder_dst.setKeep3(keep3);
	    builder_dst.setKeep4(keep4);

	    String[] GrpList_ = ParGrpTelno.split("#");
	    for (String tmp : GrpList_) {
		if (tmp != null && !tmp.equals("")) {
		    builder_dst.addParGrpTelno(tmp);
		}
	    }
	    
	    // wsy 2090520 使用json结构包含组的额外数据信息
	    try {
		builder_dst.setKeep2(JSON.toJSONString(groupDataInfo));
	    } catch (Exception e) {
		e.printStackTrace();
	    }

	    Message pbMessage = builder_dst.build();
	    return pbMessage;
	} catch (Exception e) {
		e.printStackTrace();
	}

	return null;
    }

    /**
     * 从probuf对象获取当前对象
     * 
     * @param builder_src
     * @return
     */
    public void dataChangeProBufDataToCommon(Message builder_src) {
	try {
	    com.proto.uuos_sdk_pb.Group builder_dst = (com.proto.uuos_sdk_pb.Group) builder_src;
	    GroupTelno = builder_dst.getGroupTelno();
	    GroupName = builder_dst.getGroupName();
	    GroupType = builder_dst.getGroupType();
	    Point_x = builder_dst.getPointX();
	    Point_y = builder_dst.getPointY();
	    ip = builder_dst.getIp();
	    port = builder_dst.getPort();
	    VoiceOpen = builder_dst.getVoiceOpen();
	    MessageOpen = builder_dst.getMessageOpen();
	    DataSerOpen = builder_dst.getDataSerOpen();
	    Supplement = builder_dst.getSupplement();
	    GCallTimer = builder_dst.getGCallTimer();
	    Priority = builder_dst.getPriority();
	    FreeTimer = builder_dst.getFreeTimer();
	    LETime = builder_dst.getLETime();
	    longitude = builder_dst.getLongitude();
	    latitude = builder_dst.getLatitude();
	    zoom = builder_dst.getZoom();
	    GroupTypeTmp = builder_dst.getGroupTypeTmp();
	    IconName = builder_dst.getIconName();
	    GroupNodeFlag = builder_dst.getGroupNodeFlag();
	    NodeNumber = builder_dst.getNodeNumber();
	    keep1 = builder_dst.getKeep1();
	    keep2 = builder_dst.getKeep2();
	    keep3 = builder_dst.getKeep3();
	    keep4 = builder_dst.getKeep4();

	    if (builder_dst.getSonGrpTelnoCount() > 0) {
		for (int index = 0; index < builder_dst.getSonGrpTelnoCount(); index++) {
		    String sonGroup = builder_dst.getSonGrpTelno(index);
		    try {
			Group group = JSON.parseObject(sonGroup, Group.class);
			sonGrpTelno.add(group);
		    } catch (Exception e) {
			//e.printStackTrace();
		    }
		}
	    }

	    //
	    StringBuilder builder = new StringBuilder();
	    for (int i = 0; i < builder_dst.getParGrpTelnoCount(); i++) {
		builder.append(builder_dst.getParGrpTelno(i));
		if (i < (builder_dst.getParGrpTelnoCount() - 1)) {
		    builder.append("#");
		}
	    }
	    ParGrpTelno = builder.toString();
	    
	    // wsy 20190520 使用json结构包含组的额外数据信息
	    try {
		if(keep2 != null && !keep2.equals("")) {
		    groupDataInfo = JSON.parseObject(keep2, GroupDataInfo.class);
		}
	    } catch (Exception e) {
		//e.printStackTrace();
	    }
	} catch (Exception e) {
	    e.printStackTrace();
	}
    }
    
    /**
     * 是否异节点组
     * @return
     */
    public boolean isOtherNodeGroup(){
	if(GroupNodeFlag == 1){
	    return true;
	}
	return false;
    }
}
